package app.sys.controller;


import app.sys.data.comm.ApiCode;
import app.sys.data.comm.ApiResult;
import app.sys.utils.JwtUtils;
import app.sys.data.entity.UserInfo;
import app.sys.service.UserInfoService;
import io.github.yedaxia.apidocs.ApiDoc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 用户登录controller
 */
@ApiDoc
@RestController
@RequestMapping("/user/base")
public class LoginController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 登录页面
     */
    @RequestMapping("/toLoginPage")
    public String toLoginPage() {
        //跳转至登录页面
        return "login.html";
    }

    /**
     * 登陆
     *
     * @param userCode
     * @param password
     * @return
     */
    @GetMapping(value = "/login")
    public ApiResult<String> login(String userCode, String password) {
        ApiResult<UserInfo> res = userInfoService.doLogin(userCode, password);
        //验证登录信息
        if (ApiCode.SUCCESS.getStatus() == res.getStatus()) {
            String sign = JwtUtils.sign(res.getData());
            return ApiResult.of(ApiCode.SUCCESS, sign);
        } else {
            return ApiResult.of(ApiCode.FAIL.getStatus(), "账号或密码错误");
        }
    }
}
